// FoldersPage.cpp

#include "StdAfx.h"

#include "resource.h"
#include "FoldersPage.h"

#include "../FileManager/HelpUtils.h"
#include "Common/Lang/LangUtils.h"

using namespace Common;

static Lang::CIDLangPair kIDLangPairs[] =
{
  { IDC_FOLDERS_STATIC_WORKING_FOLDER,    0x01000210 },
  { IDC_FOLDERS_WORK_RADIO_SYSTEM,        0x01000211 },
  { IDC_FOLDERS_WORK_RADIO_CURRENT,       0x01000212 },
  { IDC_FOLDERS_WORK_RADIO_SPECIFIED,     0x01000213 },
  { IDC_FOLDERS_WORK_CHECK_FOR_REMOVABLE, 0x01000214 }
};

static const int kWorkModeButtons[] =
{
  IDC_FOLDERS_WORK_RADIO_SYSTEM,
  IDC_FOLDERS_WORK_RADIO_CURRENT,
  IDC_FOLDERS_WORK_RADIO_SPECIFIED
};

static const int kNumWorkModeButtons = sizeof(kWorkModeButtons) / sizeof(kWorkModeButtons[0]);
 
BOOL CFoldersPage::OnInitDialog(CWindow wndFocus, LPARAM lInitParam)
{
  Lang::LangSetDlgItemsText(HWND(*this), kIDLangPairs, sizeof(kIDLangPairs) / sizeof(kIDLangPairs[0]));
  m_WorkDirInfo.Load();

  CButton wnd = GetDlgItem(IDC_FOLDERS_WORK_CHECK_FOR_REMOVABLE);
  wnd.SetCheck(m_WorkDirInfo.ForRemovableOnly);  
  
  CheckRadioButton(kWorkModeButtons[0], kWorkModeButtons[kNumWorkModeButtons - 1],
      kWorkModeButtons[static_cast<int>(m_WorkDirInfo.Mode)]);

  m_WorkPath = GetDlgItem(IDC_FOLDERS_WORK_EDIT_PATH);
  m_ButtonSetWorkPath = GetDlgItem(IDC_FOLDERS_WORK_BUTTON_PATH); 
  m_WorkPath.SetWindowText(m_WorkDirInfo.Path);

  MyEnableControls();
  
  return TRUE;
}

int CFoldersPage::GetWorkMode() const
{
	CButton wnd;
  for (int i = 0; i < kNumWorkModeButtons; i++)
  {
	  wnd = GetDlgItem(kWorkModeButtons[i]);
	  if(wnd.GetCheck())
		  return i;
  }
      
  throw 0;
  return -1;
}

void CFoldersPage::MyEnableControls()
{
	bool enablePath = (GetWorkMode() == static_cast<int>(Common::Archive::WorkDir::Mode::kSpecified));
  m_WorkPath.EnableWindow(enablePath);
  m_ButtonSetWorkPath.EnableWindow(enablePath);
}

void CFoldersPage::GetWorkDir(Common::Archive::WorkDir::CInfo &workDirInfo)
{
  m_WorkPath.GetWindowText(workDirInfo.Path);
  CButton wnd = GetDlgItem(IDC_FOLDERS_WORK_CHECK_FOR_REMOVABLE);
  workDirInfo.ForRemovableOnly = (bool)wnd.GetCheck();
  workDirInfo.Mode = Common::Archive::WorkDir::Mode(GetWorkMode());
}


void CFoldersPage::ModifiedEvent()
{
	SetModified();
}

LRESULT CFoldersPage::OnChangeWorkModeButtons(WORD wNotifyCode, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	MyEnableControls();
    ModifiedEvent();
	return TRUE;
}

LRESULT CFoldersPage::OnChange(WORD wNotifyCode, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	ModifiedEvent();
    return TRUE;
}

LRESULT CFoldersPage::OnEditChange(WORD wNotifyCode, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  if (wNotifyCode == EN_CHANGE)
  {
    ModifiedEvent();
    return TRUE;
  }
  return FALSE;
}

LRESULT CFoldersPage::OnFoldersWorkButtonPath(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
  CString currentPath;
  m_WorkPath.GetWindowText(currentPath);
  CString title = Lang::LangString(IDS_FOLDERS_SET_WORK_PATH_TITLE, 0x01000281);
  CFolderDialog dlg(*this, title, BIF_RETURNONLYFSDIRS|BIF_NEWDIALOGSTYLE | BIF_STATUSTEXT);
  if(!currentPath.IsEmpty())
	  dlg.SetInitialFolder(currentPath);
  if (IDOK == dlg.DoModal())
  {
	  
	  m_WorkPath.SetWindowText(dlg.GetFolderPath());
  }
    
  return OnChange(wNotifyCode, wID, hWndCtl, bHandled);
}

int CFoldersPage::OnApply()
{
  GetWorkDir(m_WorkDirInfo);
  m_WorkDirInfo.Save();
  return TRUE;
}

static LPCTSTR kFoldersTopic = _T("fm/plugins/7-zip/options.htm#folders");

void CFoldersPage::OnHelp()
{
  ShowHelpWindow(NULL, kFoldersTopic);
}
